package com.graduation.lastproject.controller.admin.AdminBasicActive;


import com.graduation.lastproject.entity.Admin;
import com.graduation.lastproject.service.admin.AdminBasicActive.AdminService;
import com.graduation.lastproject.util.shiro.LoginsToken;
import com.graduation.lastproject.util.shiro.PasswordUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Value("${login.admin}")
    private String loginType;

    @RequestMapping(value = "{page}")
    public Object topages(@PathVariable String page,HttpServletRequest request){
        //@PathVariable 映射url地址占位符的参数 到方法参数中；
      /*  if(page.equals("index")){
            String adminName=request.getSession().getAttribute("Admin")+"";
        }*/
      /*  if(page.equals("aupdata")){
            request.setAttribute("type","修改");
            return "admin/login";
        }*/
        String pages="admin/"+page;
        return pages;
    }


    //管理员登录
    @RequestMapping(value = "/alogin")
    @ResponseBody
    public Object login(Admin admin, Model model){
        Map<String,Object> map=new HashMap<>() ;
        //获取subject
        Subject subject= SecurityUtils.getSubject();
        //创建令牌，获取token  /*PasswordUtil.md5Hash(admin.getApassword(),"admin",2)*/
        LoginsToken loginsToken=new LoginsToken(admin.getArealname(),PasswordUtil.md5Hash(admin.getApassword(),"admin",2),loginType);
        try {
            subject.login(loginsToken);
            map.put("msg","登录成功");
            return map;
        }catch (UnknownAccountException e){
            map.put("msg","登录名未注册,登录失败");
            return map;
        }catch (IncorrectCredentialsException e){
             map.put("msg","密码错误,登录失败");
            ; return map;
        }
    }

    //管理员信息修改
    @RequestMapping(value = "/aupdata", method= RequestMethod.POST)
    @ResponseBody
    public Object aupdata(HttpServletRequest request){
        Map<String,Object> result=new HashMap<>() ;
        Map<String,Object> map =parameters(request);
        String string = map.get("apassword").toString();
        map.put("apassword", PasswordUtil.md5Hash(string,"admin",2));
        int i=adminService.uploadAdminInfo(map);
        result.put("msg" ,i==1 ? "修改成功" : "修改失败");
        logout();
        return result;
    }

    //管理员退出 清除浏览器管理员Session
    @RequestMapping("/alogout")
    public Object logout(){
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        session.removeAttribute("adminInfo");
        session.removeAttribute("admin");
        return "admin/login";
    }

    /** request转map */
    private final Map<String, Object> parameters(HttpServletRequest request) {
        Map<String, Object> parameters = new HashMap<String, Object>();
        if (request != null) {
            Enumeration<String> keys = request.getParameterNames();
            while (keys.hasMoreElements()) {
                String key = keys.nextElement();
                try {
                    parameters.put(key, request.getParameter(key));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return parameters;
    }


}
